<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/6/3
 * Time: 20:14
 */
namespace app\shopping\controller;

use app\common\model\Base;
use app\shopping\model\Indent;

class Indentpage extends Base{
    function __construct()
    {
        parent::__construct();

    }

    public function index(){
        return view('index');
    }
    /**
     *
     * @api {get} /shopping/indentpage/getIndent  获取商品订单
     * @apiDescription 获取商品订单
     * @apiGroup shopping
     * @apiVersion 0.1.0
     * @apiParam {string} [page] 没有则默认为0
     * @apiParam {string} indent_mode  1：待付款，2：待发货或已付款，3：待收货，4：交易成功，5：交易取消，6：交易关闭,7：待处理,8:已处理,9：已退款
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "page": 17754928306，
     *       "indent_mode": "1"，
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} count 数据总数量（用于分页操作）
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function getIndent(){
        $indent = new Indent();
        $page = $_GET["page"];

        $indent_mode = $_GET["indent_mode"];

        if(empty($indent_mode)){
            echo echoJson(0, "error");
        }

        $page = (intval($page))*10;

        if(empty($page)){
            $sql = "select * from sopp_indent where indent_state='$indent_mode' LIMIT 0,10";
        }else{
            $sql = "select * from sopp_indent where indent_state='$indent_mode' LIMIT ".$page.",10";
        }
        $sql1 = "select COUNT(*) AS count from sopp_indent";


        $data = $this->mysql->selectSQLAll($sql);

        for($i=0;$i<10;$i++){
            $result = $indent->getIndentComm($data[$i]["indent_id"]);

            if($result)
                $data[$i]["comm"] = $result;
        }


       $arr = array(
            "code"=>"1",
            "count"=>$this->mysql->selectSQL($sql1)["count"],
            "data"=>$data,
        );
        echo json_encode($arr);
    }

    public function getComm(){
/*        $arr = ["a"=>"b"];
        $arr["c"]=["d"=>"a"];
        var_dump($arr);*/
        $indent = new Indent();
        echoJson(1,$indent->getIndentComm($_GET["indent_id"]));
    }

    
    public function status(){
        $indent = new Indent();
        $indent_id = $_GET["indent_id"];
        $status = $_GET["status"];

        if(empty($status)){
            $old_status = $indent->getIndentStatus($indent_id);     //获取订单此时状态
            if($old_status >= 4){
                echoJson(0, "更新失败");
            }else
            if($indent->ChangeIndentStatus($indent_id,$old_status+1)){
                echoJson(1, $indent->getIndent_state($old_status+1));
            }
        }else{
            if($indent->ChangeIndentStatus($indent_id,$status)){
                echoJson(1, $indent->getIndent_state($status));
            }
        }
    }
}